//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"
//3.可变参数
func outArray (s: String...) -> [String] {
    //    var result = [String]()
    //    for str in s {
    //        result.append(str)
    //    }
    //    return result
    return s
}

outArray("sa", "sb", "sc")
outArray("xb", "think", "pad", "mac", "book")

//4.交换函数
func swip<T> (inout a: T, inout b: T) {
    var t : T
    t = a
    a = b
    b = t
}

var a = 7
var b = 8
swip(&a, b: &b)
print("\(a), \(b)")

var a1 = "sb"
var a2 = "bs"
swip(&a1, b: &a2)
print("\(a1), \(a2)")

//: 5写一个函数,使用数量不定的谓语作为参数,并返回另一个函数--这个函数将所有谓词组合在一起
func outAllString(s: String...) -> () -> [String] {
    func out() -> [String] {
        return s
    }
    return out
}

let outFunc01 = outAllString("1", "3", "bc", "sb")
print(outFunc01())

//: 6写一个函数,使用一个数组和一个条件闭包作为参数,返回一个数组,这个数组中仅仅包含原数组中满足条件闭包的值
func flatMapArray(arr: [Int], closure: (a: Int) -> Int?) -> [Int] {
    var resultArr = [Int]()
    for aInt in arr {
        if let result = closure(a: aInt) {
            resultArr.append(result)
        }
    }
    return resultArr
}
func closure01(a: Int)(b: Int) -> Int? {
    var r: Int?
    let s = b % a
    if s == 0 {
        r = b
    }
    return r
}

let arr01 = [3, 4, 5, 7, 8, 10, 13, 15, 18, 19, 21]
let closureFunc01 = closure01(2)
let closureFunc02 = closure01(3)
let closureFunc03 = closure01(4)
let closureFunc04 = closure01(5)
let closureFunc05 = closure01(6)

flatMapArray(arr01, closure: closureFunc01)
flatMapArray(arr01, closure: closureFunc02)
flatMapArray(arr01, closure: closureFunc03)
flatMapArray(arr01, closure: closureFunc04)
flatMapArray(arr01, closure: closureFunc05)

//: 7.使用map()函数将Double数组中的值转变为原值除以数组平均值
let arr02 = [3.4, 4.7, 5.3, 3.8, 6.5, 1.4, 3.45, 8.9]
let r02 = arr02.reduce(0, combine: +) / Double(arr02.count)
arr02.map( { $0 / r02 } )






//: 8.使用本章介绍的foldLeft()函数,写一个函数.该函数使用一个数组作为参数,同时返回另一个数组,数组中的元素顺序和原来相反
func foldLeft<T, U>(array: [T], initialValue: U, closure: (U, T) -> U) -> U {
    var foldValue = initialValue
    for e in array {
        foldValue = closure(foldValue, e)
    }
    return foldValue
}

func reverseArray<T>(array: [T]) -> [T] {
    //    return array.reverse()
    var result = [T]()
    for i in 0...(array.count - 1) {
        result.append(array[array.count - 1 - i])
    }
    return result
}

let arr03 = [3.4, 3.5, 1.2, 4.5, 1.9, 4.5, 1.2, 3.5, 4.5, 2.9, 3.7, 2.9]
let result03 = reverseArray(arr03)

print(arr03)
func clearRepeat<T: Equatable>(array: [T]) -> [T] {
    if array.count == 0 {
        return [T]()
    }
    
    var result = [T]()
    var read = [Int: Bool]()
    (0..<array.count).forEach {
        read[$0] = false
    }
    
    for i in 0..<array.count {
        if let r = read[i] {
            if !r {
                result.append(array[i])
                for j in (i + 1)..<array.count {
                    if let s = read[j] {
                        if !s {
                            if array[i] == array[j] {
                                print("a[\(i)] = \(array[i]), a[\(j)] = \(array[j])")
                                read[j] = true
                                
                            }
                        }
                    }
                }
            }
        }
    }
    return result
}

let result04 = clearRepeat(arr03)

//
//: [Next](@next)
